package com.eliauk.demo.controller.write;

import com.alibaba.excel.EasyExcel;
import com.eliauk.demo.entity.wirte.IndexData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("write3")
public class write3Controller {

    List<IndexData> data = ListData.indexData();

    /**
     * 导出到指定列，比如我想字符串导出到第二列，日期导出到第一列，那么只要在实体类中设置就好了
     * @return
     */
    @GetMapping("download")
    public String exportData(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), IndexData.class)
                .sheet("模板")
                .doWrite(data);
        return "success";
    }
}
